package com.japp.sys.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.japp.base.BaseService;
import com.japp.sys.bean.SelectBean;
import com.japp.sys.mapper.SelectMapper;
import com.japp.tools.common.Common;
import com.japp.tools.common.JappLog;
import com.japp.tools.page.Page;
@Service
@Transactional(rollbackFor = Exception.class)
public class SelectService implements BaseService {

	@Autowired
	private SelectMapper selectMapper;
	
	public SelectBean get(Serializable pk) {
		return selectMapper.get(pk);
	}

	
	public Page<SelectBean> getPage(Page<SelectBean> po, SelectBean obj) {
		Page<SelectBean> page = null;
		try {
			page = selectMapper.getPage(po, obj);
		} catch (Exception e) {
			e.printStackTrace();
			return po;
		}
		return page;
	}

	
	public List<SelectBean> querySelectByType(String type) {
		return selectMapper.querySelectByType(type);
	}

	
	public SelectBean findSelectById(int id) {
		return selectMapper.findSelectById(id);
	}

	
	public int insertSelect(SelectBean select) {
		try {
			int rel = selectMapper.insertSelect(select);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("",e);
		}
		return 0;
	}

	
	public int updateSelect(SelectBean select) {
		try {
			int rel = selectMapper.updateSelect(select);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("",e);
		}
		return 0;
	}

	
	public int deleteSelectById(String[] keys) {
		return selectMapper.deleteSelectById(keys);
	}

	public List<SelectBean> querySelectByTypeToJson(String type) {
		List<SelectBean> list = null;
		try {
			list = selectMapper.querySelectByTypeToJson(type);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Map<String, String> getSelMapByString(String str){
		if(str==null||str.trim().length()==0){
			return null;
		}
		List<SelectBean> list = null;
		Map<String, String> map = new HashMap<String, String>();
		try {
			String[] keys  = str.split(",");
			int i,len = keys.length;
			for(i=0;i<len;i++){
				list = selectMapper.querySelectByTypeToJson(keys[i]);
				str = Common.formtSelectToJson(list);
				map.put("sel_"+keys[i], str);
			}
			
		} catch (Exception e) {
			JappLog.log.error("获取下拉编号"+str+"错误",e);
		}
		return map;
		
	}
	
	public String querySelectTypeAll() {
		try {
			List<SelectBean> list = selectMapper.querySelectTypeAll();
			if(list!=null&&list.size()>0){
				String str = Common.formtSelectToJson(list);
				return str;
			}
		} catch (Exception e) {
			JappLog.log.error("",e);
		}
		return "";
	}

}
